package com.atguigu.lease.web.admin.controller.apartment;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.ApartmentInfo;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.admin.service.ApartmentInfoService;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Tag(name = "公寓信息管理")
@RestController
@RequestMapping("/admin/apartment")
public class ApartmentController {
    @Autowired
    private ApartmentInfoService apartmentInfoService;

    @Operation(summary = "保存或更新公寓信息")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody ApartmentSubmitVo apartmentSubmitVo) {

        apartmentInfoService.saveOrUpdateItem(apartmentSubmitVo);
        return Result.ok();
    }

    /**
     * 业务说明:
     *      1.要求根据传递的 省市区id 进行动态sql查询
     *      2.根据页数和每页的条数 实现分页查询    MP自带分页 需要配置拦截器!!!
     *      3.判断返回值数据
     *          3.1 主表  公寓表 ApartmentInfo
     *          3.2 从表  房间表  room_info
     *          3.3 从表  租约表  lease_agreement
     *
     * @param current
     * @param size
     * @param queryVo
     * @return
     */
    @Operation(summary = "根据条件分页查询公寓列表")
    @GetMapping("pageItem")
    public Result<IPage<ApartmentItemVo>> pageItem(@RequestParam long current, @RequestParam long size, ApartmentQueryVo queryVo) {

        IPage<ApartmentItemVo> page = apartmentInfoService.findPageList(current,size,queryVo);
        return Result.ok(page);
    }

    /**
     * 参数分析:
     *      1.id 公寓id号
     *      2.返回值  ApartmentDetailVo
     *          2.1 ApartmentInfo 公寓表信息
     *          2.2 graphVoList  图片信息
     *          2.3 labelInfoList 标签信息
     *          2.4 facilityInfoList 配套信息
     *          2.5 feeValueVoList 杂费信息
     *
     * @param id
     * @return
     */
    @Operation(summary = "根据ID获取公寓详细信息")
    @GetMapping("getDetailById")
    public Result<ApartmentDetailVo> getDetailById(@RequestParam Long id) {

        ApartmentDetailVo apartmentDetailVo = apartmentInfoService.findDetailById(id);
        return Result.ok(apartmentDetailVo);
    }

    @Operation(summary = "根据id删除公寓信息")
    @DeleteMapping("removeById")
    public Result removeById(@RequestParam Long id) {
        apartmentInfoService.removeApartmentById(id);
        return Result.ok();
    }

    @Operation(summary = "根据id修改公寓发布状态")
    @PostMapping("updateReleaseStatusById")
    public Result updateReleaseStatusById(@RequestParam Long id, @RequestParam ReleaseStatus status) {

        LambdaUpdateWrapper<ApartmentInfo> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.set(ApartmentInfo::getIsRelease,status)
                .eq(ApartmentInfo::getId,id);
        apartmentInfoService.update(lambdaUpdateWrapper);
        return Result.ok();
    }

    @Operation(summary = "根据区县id查询公寓信息列表")
    @GetMapping("listInfoByDistrictId")
    public Result<List<ApartmentInfo>> listInfoByDistrictId(@RequestParam Long id) {

        LambdaQueryWrapper<ApartmentInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApartmentInfo::getDistrictId,id);
        List<ApartmentInfo> apartmentInfoList = apartmentInfoService.list(queryWrapper);
        return Result.ok(apartmentInfoList);
    }
}














